package cn.wolfcode.wms.service.impl;

import cn.wolfcode.wms.domain.Role;
import cn.wolfcode.wms.mapper.RoleMapper;
import cn.wolfcode.wms.query.QueryObject;
import cn.wolfcode.wms.service.IRoleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RoleServiceImpl implements IRoleService {
    @Autowired
    private RoleMapper roleMapper;
    public void save(Role role,Long[] ids) {
        roleMapper.insert(role);
        if (ids != null){
            for (Long id : ids) {
                roleMapper.insertRealtion(role.getId(),id);
            }
        }
    }

    public void update(Role role,Long[] ids) {
        roleMapper.deleteRealtion(role.getId());
        roleMapper.updateByPrimaryKey(role);
        if (ids != null){
            for (Long id : ids) {
                roleMapper.insertRealtion(role.getId(),id);
            }
        }
    }

    public void delete(Long id) {
        roleMapper.deleteRealtion(id);
        roleMapper.deleteByPrimaryKey(id);
    }

    public Role get(Long id) {
        return roleMapper.selectByPrimaryKey(id);
    }

    public List<Role> list() {
        return roleMapper.selectAll();
    }

    public PageInfo<Role> query(QueryObject qo) {
//        调用分页包
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Role> list = roleMapper.queryForList(qo);
        return new PageInfo<Role>(list);
    }

}
